import { defineStore } from "pinia";
import { AutoChannelSetResponse, ComApiTableAPI } from "@/api/hmi/com-api-table.api";

export const useComApiTableStore = defineStore("comApiTable", () => {
  function fetchList(baseUrl: string, queryParams: any) {
    return new Promise<any[]>((resolve, reject) => {
      ComApiTableAPI.getOffsetList(baseUrl, queryParams)
        .then((res) => {
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  function batchUpdate(baseUrl: string, data: any) {
    return new Promise<any[]>((resolve, reject) => {
      ComApiTableAPI.batchUpdate(baseUrl, data)
        .then((res) => {
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  function batchReset(baseUrl: string, data: any) {
    return new Promise<any[]>((resolve, reject) => {
      ComApiTableAPI.batchReset(baseUrl, data)
        .then((res) => {
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  function batchTrans(baseUrl: string, data: any) {
    return new Promise<any[]>((resolve, reject) => {
      ComApiTableAPI.batchTrans(baseUrl, data)
        .then((res) => {
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  function autoChannelSet(baseUrl: string, data: any) {
    return new Promise<AutoChannelSetResponse>((resolve, reject) => {
      ComApiTableAPI.autoChannelSet(baseUrl, data)
        .then((res) => {
          resolve(res);
        })
        .catch((error) => {
          reject(error);
        });
    });
  }

  return { fetchList, batchUpdate, batchReset, batchTrans, autoChannelSet };
});

export function useComApiTableStoreHook() {
  return useComApiTableStore();
}
